import React, { useState } from 'react'
import Myinput from "../../components/myinput/Myinput.jsx"
import Verfy from "../../components/verfy/Verfy.jsx"
import logo from "./img/logo.jpg";
import axios from "axios"
import style from "./css/style.module.css"
function Index(props) {
    console.log(props.history.location.state)
    let routes = props.history.location.state
    const [tel, settel] = useState(routes ? routes.tel : "")
    const [pswd, setpswd] = useState(routes ? routes.pswd : "")
    const [verify, setverify] = useState("")
    // 登录逻辑点击登录按钮访问/login接口
    let login = async () => {
        // console.log(tel,pswd,verify)
        let url = `/login`
        let res = await axios.post(url, { tel, pswd, verify })
        // console.log(res.data);
        //返回2002代码就登录成功
        if (res.data.code == 2002) {
            window.localStorage.setItem("isLogin", "true")
            props.history.push("/")
        }
        //返回4004代码密码错误
        else if (res.data.code == 4004) {
            alert(res.data.info)
        }
        //返回2002代码验证码错误
        else if (res.data.code == 4001) {
            alert(res.data.info)
        }
        //返回4003code码就没有注册跳到注册页面
        else if (res.data.code == 4003) {
            alert(res.data.info, "即将跳转到注册页面")
            // 自己写的组件没有props.history
            props.history.push('/register')
        }
    }
    function toHome() {
        props.history.push({ pathname: "/", state: {} });
    }
    return (
        <div className={style.parentbox}>
            <div className={style.leftBox}>
                <img src={logo} alt="" onClick={toHome} />
            </div>
            <div className={style.box}>
                <h1 className={style.head}>登录页面</h1>

                <div className={style.titlename}>电话</div>
                {/* 传个数组给子组件 */}
                <Myinput vtype="text" vmodel={[tel, settel]} ></Myinput>

                <div className={style.titlename}>密码 </div>
                <Myinput vtype='password' vmodel={[pswd, setpswd]}></Myinput>

                <div className={style.titlename}>验证码</div>
                <Myinput vtype='text' vmodel={[verify, setverify]}></Myinput>
                <div><Verfy /></div>
                <button className={style.loginbtn} onClick={login}>登录</button>
            </div>
        </div>
    )
}

export default Index
